package com.lenovo.smarttraffic.ui.activity.stage5;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CheckedTextView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.BusStationInfoBean;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.ui.adapter.MyVpAdapter;
import com.lenovo.smarttraffic.ui.fragment.stage5.BusFindFragment;
import com.lenovo.smarttraffic.util.HttpUtils;
import com.lenovo.smarttraffic.util.Utils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class BusFind2Activity extends BaseActivity {

    public static List<BusStationInfoBean.ROWSDETAILBean> stationList;

    private List<Fragment> fragments = new LinkedList<>();

    private Toolbar toolbar;
    private TextView user_tv;

    private BarChart barChart;
    private FrameLayout vp_parent;
    private ViewPager viewPager;
    private LinearLayout indicate;
    private CheckedTextView tv1, tv2;

    private BarData barData;
    private List<BarEntry> barEntries1 = new ArrayList<>();
    private List<BarEntry> barEntries2 = new ArrayList<>();
    private int index = 0;
    private List<IBarDataSet> barDataSets = new ArrayList<>();
    private BarDataSet barDataSet1;
    private BarDataSet barDataSet2;

    private Gson gson = new Gson();

    private Handler handler = new Handler();

    @Override
    protected int getLayout() {
        return R.layout.activity_bus_find2;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener((v) -> finish());

        user_tv = findViewById(R.id.user_tv);
        user_tv.setText("当前用户：" + MainActivity.userName);

        barChart = findViewById(R.id.bar_chart);
        vp_parent = findViewById(R.id.vp_parent);
        viewPager = findViewById(R.id.view_pager);
        indicate = findViewById(R.id.indicate_container);

        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);

        tv1.setOnClickListener(v -> {
            if (!tv1.isChecked()) {
                tv1.setChecked(true);
                tv2.setChecked(false);

                tv1.setTypeface(Typeface.DEFAULT_BOLD);
                tv2.setTypeface(Typeface.DEFAULT);

                barChart.setVisibility(View.VISIBLE);
                vp_parent.setVisibility(View.GONE);
            }
        });
        tv2.setOnClickListener(v -> {
            if (!tv2.isChecked()) {
                tv1.setChecked(false);
                tv2.setChecked(true);

                tv1.setTypeface(Typeface.DEFAULT);
                tv2.setTypeface(Typeface.DEFAULT_BOLD);

                barChart.setVisibility(View.GONE);
                vp_parent.setVisibility(View.VISIBLE);
            }
        });

        initChart();
        initVp();
    }

    private void initChart() {
        Description description = new Description();
        description.setEnabled(false);
        barChart.setDescription(description);

        barEntries1.add(new BarEntry(0, 0));
        barDataSet1 = new BarDataSet(barEntries1, "1");

        barEntries2.add(new BarEntry(0, 0));
        barDataSet2 = new BarDataSet(barEntries2, "2");

        barDataSets.add(barDataSet1);
        barDataSets.add(barDataSet2);

        barData = new BarData(barDataSets);
        barData.setBarWidth(.25f);
        barData.setDrawValues(false);

        XAxis xAxis = barChart.getXAxis();
        xAxis.setTextSize(17);
        xAxis.setTextColor(Color.BLACK);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//x轴显示在底下
        xAxis.setDrawGridLines(false);//隐藏x轴辅助线
        xAxis.setAxisLineWidth(2);
        xAxis.setLabelCount(2);
        xAxis.setAvoidFirstLastClipping(true);
        //x轴格式转换
        xAxis.setValueFormatter((value, axis) -> {
            Log.e("23213213", value + "");
            return value < 3 ? "1号公交车" : "2号公交车";
        });

        YAxis yAxis = barChart.getAxisLeft();
        yAxis.setTextSize(15);
        yAxis.setTextColor(Color.BLACK);
        yAxis.setDrawGridLines(false);
        yAxis.setAxisLineWidth(2);

        barChart.getAxisRight().setEnabled(false);//隐藏y轴辅助线
        barChart.setTouchEnabled(false);
        barChart.setData(barData);//把线条设置给你的lineChart上

        runnable.run();
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            HttpUtils.post("get_bus_station_info", new HttpUtils().add("UserName", MainActivity.userName), response -> {
                BusStationInfoBean busStationInfoBean = gson.fromJson(response.toString(), BusStationInfoBean.class);
                stationList = busStationInfoBean.getROWS_DETAIL();

                int i1 = stationList.get(0).getDistance();
                int i2 = stationList.get(1).getDistance();
                int i3 = stationList.get(2).getDistance();
                int i4 = stationList.get(3).getDistance();

                barEntries1.clear();
                barEntries1.add(new BarEntry(0, i1));
                barEntries1.add(new BarEntry(1, i2));

                if (i1 > i2) {
                    barDataSet1.setColors(Utils.getColor(BusFind2Activity.this, R.color.Yellow), Utils.getColor(BusFind2Activity.this, R.color.Green));
                } else {
                    barDataSet1.setColors(Utils.getColor(BusFind2Activity.this, R.color.Green), Utils.getColor(BusFind2Activity.this, R.color.Yellow));
                }
                barDataSet1.notifyDataSetChanged();

                barEntries2.clear();
                barEntries2.add(new BarEntry(3, i3));
                barEntries2.add(new BarEntry(4, i4));
                if (i3 > i4) {
                    barDataSet2.setColors(Utils.getColor(BusFind2Activity.this, R.color.Yellow), Utils.getColor(BusFind2Activity.this, R.color.Green));
                } else {
                    barDataSet2.setColors(Utils.getColor(BusFind2Activity.this, R.color.Green), Utils.getColor(BusFind2Activity.this, R.color.Yellow));
                }
                barDataSet2.notifyDataSetChanged();

                barData.notifyDataChanged();
                barChart.notifyDataSetChanged();
                barChart.invalidate();
            });
            handler.postDelayed(runnable, 3000);
        }
    };

    private void initVp() {
        for (int i = 0; i < 3; i++) {
            fragments.add(new BusFindFragment().setType(i));
        }

        viewPager.setOffscreenPageLimit(fragments.size());
        viewPager.setAdapter(new MyVpAdapter(getSupportFragmentManager(), fragments));

        for (int i = 0; i < fragments.size(); i++) {
            ImageView imageView = new ImageView(this);
            imageView.setPadding(10, 0, 10, 0);
            if (i == 0) {
                imageView.setImageResource(R.drawable.indicate_selected);
            } else {
                imageView.setImageResource(R.drawable.indicate);
            }
            imageView.setTag(i);
            indicate.addView(imageView);
        }

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < fragments.size(); i++) {
                    ImageView imageView = indicate.findViewWithTag(i);
                    if (i == position) {
                        imageView.setImageResource(R.drawable.indicate_selected);
                    } else {
                        imageView.setImageResource(R.drawable.indicate);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

}
